package com.yunwaikeji.gy_tool_library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.utils.GYUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYViewPager extends ViewPager {
    private boolean isScroll = true;
    private boolean showAnimation = true;
    private List<View> noScrollViewList = new ArrayList<>();

    public GYViewPager(@NonNull Context context) {
        this(context, null);
    }

    public GYViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attributeSet) {
        if (attributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.GYViewPager);
            isScroll = typedArray.getBoolean(R.styleable.GYViewPager_canSlide, isScroll);
            showAnimation = typedArray.getBoolean(R.styleable.GYViewPager_showAnimation, showAnimation);
            typedArray.recycle();
        }
        setScroll(isScroll);
        setShowAnimation(showAnimation);
    }

    public GYViewPager setShowAnimation(boolean showAnimation) {
        this.showAnimation = showAnimation;
        return this;
    }

    public boolean isScroll() {
        return isScroll;
    }

    public boolean isShowAnimation() {
        return showAnimation;
    }

    public GYViewPager addNoScrollViews(List<View> noScrollViewList) {
        this.noScrollViewList.addAll(noScrollViewList);
        return this;
    }

    public GYViewPager addNoScrollViews(View... views) {
        return addNoScrollViews(Arrays.asList(views));
    }

    public GYViewPager setNoScrollViews(List<View> noScrollViewList) {
        this.noScrollViewList.clear();
        return addNoScrollViews(noScrollViewList);
    }

    public GYViewPager setNoScrollViews(View... views) {
        return setNoScrollViews(Arrays.asList(views));
    }

    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item, showAnimation);
    }

    /**
     * 1.dispatchTouchEvent一般情况不做处理
     * ,如果修改了默认的返回值,子孩子都无法收到事件
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);   // return true;不行
    }

    /**
     * 是否拦截
     * 拦截:会走到自己的onTouchEvent方法里面来
     * 不拦截:事件传递给子孩子
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // return false;//可行,不拦截事件,
        // return true;//不行,孩子无法处理事件
        //return super.onInterceptTouchEvent(ev);//不行,会有细微移动
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                for (View view : noScrollViewList) {
                    int left = GYUtils.getLocationScreenX(view);
                    int top = GYUtils.getLocationScreenY(view);
                    if (ev.getX() > left && ev.getX() < top + view.getWidth() && ev.getY() > top && ev.getY() < top + view.getHeight()) {
                        return false;
                    }
                }
                break;
        }
        if (isScroll) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return false;
        }
    }

    private PointF sp = new PointF();

    /**
     * 是否消费事件
     * 消费:事件就结束
     * 不消费:往父控件传
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //return false;// 可行,不消费,传给父控件
        //return true;// 可行,消费,拦截事件
        //super.onTouchEvent(ev); //不行,
        //虽然onInterceptTouchEvent中拦截了,
        //但是如果viewpage里面子控件不是viewgroup,还是会调用这个方法.
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                for (View view : noScrollViewList) {
                    int left = GYUtils.getLocationScreenX(view);
                    int top = GYUtils.getLocationScreenY(view);
                    if (ev.getX() > left && ev.getX() < top + view.getWidth() && ev.getY() > top && ev.getY() < top + view.getHeight()) {
                        return false;
                    }
                }
                break;
        }
        if (isScroll) {
            return super.onTouchEvent(ev);
        } else {
            return true;// 可行,消费,拦截事件
        }
    }

    public GYViewPager setScroll(boolean scroll) {
        isScroll = scroll;
        return this;
    }
}
